package com.preguicoso.client.telaInicial;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ListBox;
import com.preguicoso.client.backend.pedidos.OrdemPedidos;
import com.preguicoso.client.busca.BuscaService;
import com.preguicoso.client.busca.BuscaServiceAsync;
import com.preguicoso.client.checkout.CheckoutService;
import com.preguicoso.client.checkout.CheckoutServiceAsync;
import com.preguicoso.client.estabelecimento.cardapio.CardapioService;
import com.preguicoso.client.estabelecimento.cardapio.CardapioServiceAsync;
import com.preguicoso.shared.RegistroErros;
import com.preguicoso.shared.entities.BairroBean;
import com.preguicoso.shared.entities.CidadeBean;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.event.dom.client.FocusEvent;

public class EscolhaCep extends Composite  {

	private static EscolhaCepUiBinder uiBinder = GWT
			.create(EscolhaCepUiBinder.class);
	private static EscolhaCep instance = null;
	@UiField TextBox cep;
	@UiField ListBox bairros;
	@UiField HTMLPanel panel;
	ArrayList<String> bairrosList = new ArrayList<String>();
	BairroBean bairroEscolhido = null;
	
	private final BuscaServiceAsync buscaService = GWT
			.create(BuscaService.class);
	private final CardapioServiceAsync cardapioService = GWT
			.create(CardapioService.class);

	private final CheckoutServiceAsync cepService = GWT.create(CheckoutService.class);
	
	interface EscolhaCepUiBinder extends UiBinder<Widget, EscolhaCep> {
	}
	
	public static EscolhaCep getInstance() {
		
			if (instance == null) {
					instance = new EscolhaCep();
			}
			return instance;
	}

	private EscolhaCep() {
		if(Cookies.getCookie("bairro")==null){
			initWidget(uiBinder.createAndBindUi(this));
			bairros.addItem("Escolha o seu bairro");
			getListBairros();
		}
		else{
			initWidget(uiBinder.createAndBindUi(this));
			bairros.addItem("Escolha o seu bairro");
			getListBairros();
		}
		
	}
	public String getBairro(){
		if(Cookies.getCookie("bairro")==null){
			History.newItem("cep");
			return null;
		}
		else{
			return Cookies.getCookie("bairro");
		}
	}
	
	public void getListBairros(){
		bairros.clear();
		bairros.addItem("Carregando Bairros...");
		cardapioService.getBairrosAtendidos((long)1,
			new AsyncCallback<List<BairroBean>>() {

				@Override
				public void onFailure(Throwable caught) {
					Window.alert(RegistroErros.CONEXAO);
				}

				@Override
				public void onSuccess(List<BairroBean> result) {
					bairros.clear();
					bairros.addItem("Escolha o seu bairro");
					for (BairroBean bb : result) {
						bairros.addItem(bb.getNome()+" - Fortaleza");
						bairrosList.add(bb.getNome());
					}
				}
			});
	}
	@UiHandler("ok")
	void onOkClick(ClickEvent event) {
		if(bairros.getSelectedIndex()!=0)
		{
			bairroEscolhido = new BairroBean();
			bairroEscolhido.setIdCidade((long)1);
			bairroEscolhido.setNome(bairros.getItemText(bairros.getSelectedIndex()));
			Cookies.setCookie("bairro",bairros.getItemText(bairros.getSelectedIndex()));
			Cookies.setCookie("bairroNome",bairros.getItemText(bairros.getSelectedIndex()));
			History.newItem("index");
			panel.setVisible(false);
		}
		else{
			cepService.getEnderecoByCep(cep.getText(), new AsyncCallback<String[]>() {
				
				@Override
				public void onSuccess(String[] result) {
					String a="";
					if(result==null){
						lightBox light = lightBox.getInstance();
						light.show(new HTMLPanel("<h2>Cep Inválido</h2>"),126,61);
					}else{
						for (String string : result) {
							a=a+" | "+string;
						}
						if(bairrosList.contains(result[1])){
							bairroEscolhido = new BairroBean();
							bairroEscolhido.setIdCidade((long)1);
							bairroEscolhido.setNome(result[1]);
							Cookies.setCookie("bairro",result[1]+" - Fortaleza");
							History.newItem("index");
							panel.setVisible(false);
						}else{
							lightBox light = lightBox.getInstance();
							NaoAtendemos nao = new NaoAtendemos();
							nao.setEndereco(result[0], result[1]);
							light.show(nao,496,181);
						}
					}
				}
				
				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}
			});
			
		}
	}
	@UiHandler("cep")
	void onCepFocus(FocusEvent event) {
		cep.setText("");
	}
}
